package com.xl.singletonPattern;

/**
 * 双重检查锁单例模式
 */
public class DoubleCheckSingleton {
    //使用volatile防止指令重排序
    private static volatile DoubleCheckSingleton singleton;
    private DoubleCheckSingleton() {
        System.out.println("创建实例...");
    }
    public static DoubleCheckSingleton getInstance() {
        if (singleton == null) {
            synchronized (DoubleCheckSingleton.class) {
                if (singleton == null) {
                    singleton = new DoubleCheckSingleton();
                }
            }
        }
        return singleton;
    }
}
